home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / 4thcmp21.zip / DEMO.4TH < prev    next >
Text File  |  1993-06-23  |  5KB  |  122 lines

  1. \ This simple program illustrates the basics of a Forth program prepared
  2. \ for ForthCMP. Comments can be anything after a backslash,
  3. ( or can be text within parenthesis on a single line )
  4. 0 #IF    Interpreted conditionals allow multiline comments, among other
  5.     things, without having to bother with the comment characters.
  6.  
  7.     Lines can be up to 127 characters long, and spaces and tabs are
  8.     ignored.
  9.  
  10.     To compile this program, the compiler, 4C.COM, must be in the
  11.     execution path, and the files DOSGO.SCR and FORTHLIB.SCR must be
  12.     either in the current directory or in a directory pointed to by
  13.     the environment varible 4LIB. For example, "SET 4LIB=d:\FCLIB"
  14.     will cause the directory d:\fclib to be used for file searches
  15.     if the file is not found in the current directory. These library
  16.     files, as all input files, are in source format. Source files have
  17.     the default extensions 4TH or SCR. Either ASCII text files, such
  18.     as this one, or Forth screen files, such as the library files, can
  19.     be used interchangably. Any "included" screen file is read in via
  20.     an implicit "1 LOAD" command.
  21. #THEN
  22.  
  23. \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  24. \                                    \
  25. \  If you like ForthCMP, please register it and get the programmers'    \
  26. \  manual plus a disk (specify size) with the current version and    \
  27. \  additional support code:                        \
  28. \                                     \
  29. \  Send a check or money order for $50.00 US to:            \
  30. \      Tom Almy                                \
  31. \      17830 SW Shasta Trail                        \
  32. \      Tualatin, OR 97072                        \
  33. \                                     \
  34. \ (To get the Software Floating Point files, you must prove ownership   \
  35. \  of an LMI Forth Software Floating Point by enclosing a copy of the   \
  36. \  first page in the manual section for Software Floating Point.)       \
  37. \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  38.  
  39. \ I80186    \ Uncomment this if you are using an 80186 or later processor
  40.  
  41. 200 SEPSSEG    \ If you uncomment this line, then the program will have a
  42.         \ separate stack segment, 200 bytes long, to hold the
  43.         \ parameter and return stacks. This directive must come
  44.         \ before the MSDOS or MSDOSEXE directive
  45.  
  46. \ ForthCMP programs must start with one of the following directives:
  47.  
  48. 100 MSDOS \ Specifies to generate a COM file, allowing for a return stack
  49.           \ size of 100 bytes. This generates a "tiny" model program.
  50. \ 1000 100 MSDOSEXE \ Specifies to generate an EXE file, allowing 1000 bytes
  51.         \ for the data segment and 100 bytes for the return stack. This 
  52.         \ generates a small model program.
  53.  
  54. \ A maximal sized program can have 63k of code, 64k of static (dictionary)
  55. \ data, and 64k of stack data. You can request additional memory segments
  56. \ from DOS for large data arrays, for instance. Except when SEPSSEG is
  57. \ specified, the parameter stack moves down into the heap. The heap can
  58. \ be allocated using HERE (or DP) and ALLOT, just like in interpreted 
  59. \ Forth. PAD floats above HERE.
  60.  
  61. \ A DRIVER option allows creating device drivers in ForthCMP.
  62.  
  63. \ There is also an option for creating ROMMABLE code for embedded 
  64. \ processors.
  65.  
  66. \ The programmer's manual describes all the options in detail, however there 
  67. \ are enough example programs that you can probably figure most of it out
  68. \ by example.
  69.  
  70. \ A ForthCMP program always contains a function called MAIN that acts as
  71. \ the main entry point. FORTCOM allows forward referencing, so we can safely
  72. \ put the MAIN function first.
  73.  
  74. 1 1 IN/OUT NEED TwoTimes    \ this tells the compiler that a forward
  75.                 \ referenced function has exactly one argument
  76.                 \ and one result value -- this allows the
  77.                 \ compiler to pass the argument and result
  78.                 \ in registers.
  79.                 \ The default is on the stack
  80.  
  81.  
  82. : MAIN
  83.     ." HELLO NEW USER!" CR CR    \ this should look standard.
  84.     ." Type a number:" #IN
  85.     ." Two times the number is: " TwoTimes . CR
  86.     ." Multiplication table"
  87.     MultTable
  88. ;                    \ returns to DOS at the end. Make
  89.                     \ certain the stack is empty, or
  90.                     \ push a 0 on stack if not sure.
  91.                     \ You can also execute BYE at any
  92.                     \ point to return. You can even
  93.                     \ return error codes, if you wish.
  94.  
  95. 1 1 IN/OUT    \ must match any forward declaration
  96.  
  97. CODE TwoTimes    \ We will use an assembly code routine to do the multiply
  98.     AX AX ADD    \ arg and result use AX
  99.     RET        \ couldn't be easier!
  100. END-CODE
  101.  
  102.  
  103. : MultTable    \ We didn't use IN/OUT here
  104.     11 1 DO
  105.         CR
  106.         11 1 DO
  107.         I J *  5 .R    \ print each entry
  108.     LOOP
  109.     LOOP
  110. ;
  111.  
  112. \ Our program is done. Now we need to include the library of 83 Standard
  113. \ functions that are not intrinsically generated.
  114. \ The generated load map shows these functions, as well as the ones above.
  115. \ You can use DEBUG to examine the generated code
  116.  
  117. INCLUDE FORTHLIB
  118.  
  119. END    \ Required last command -- writes out the compiled program
  120.  
  121. \ Now from the command line, execute "4c demo"
  122.